package com.sitc.administrator.application.network;



//import com.readystatesoftware.chuck.ChuckInterceptor;
import com.sitc.administrator.application.domain.LoginRes;
import com.sitc.administrator.application.domain.StationInfos;
import com.sitc.administrator.application.util.StaticValue;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;


import io.reactivex.Observable;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class Api {
    private static final String ID = "API";
    private static Api instance;
    private static Service service;

    Api() {
        // 创建 OKHttpClient
        //File file = new File(AppContext.getContext().getCacheDir(), "http");
        //创建缓存
        //Cache cache = new Cache(file, 1024 * 1024 * 1000);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //builder.cache(cache);
        builder.connectTimeout(StaticValue.TIME_OUT, TimeUnit.SECONDS);//连接超时时间
        builder.writeTimeout(StaticValue.TIME_OUT, TimeUnit.SECONDS);//写操作 超时时间
        builder.readTimeout(StaticValue.TIME_OUT, TimeUnit.SECONDS);//读操作超时时间

		//builder.addInterceptor(new HttpLoggingInterceptor());
		//builder.addNetworkInterceptor(new StethoInterceptor());

		///builder.addInterceptor(new ChuckInterceptor(AppContext.getContext()));
        //builder.addInterceptor(FORCE_CACHE_NETWORK_DATA_INTERCEPTOR);
        //builder.addNetworkInterceptor(FORCE_CACHE_NETWORK_DATA_INTERCEPTOR);

        //公共请求参数
        builder.addNetworkInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {

                Request request = chain.request();
//              SharedPreferencesUtil sp = SharedPreferencesUtil.getInstance();
//                if (sp.isLogin()) {
//                    String userId = sp.getUserId();
//                    String token = sp.getToken();
//
//                    if (LogUtil.isDebug) {
//                        LogUtil.d("token:" + token + "," + userId);
//                    }
//
//                    request = chain.request().newBuilder()
//                            .addHeader("User", userId)
//                            .addHeader("Authorization", token)
//                            .build();
//                }
                return chain.proceed(request);
            }
        });


        //String baseUrl = "http://120.27.140.246:8081/sty_v2/";

        // 添加公共参数拦截器
//        Retrofit retrofit = new Retrofit.Builder()
//                .client(builder.build())
//                .baseUrl(StaticValue.ENDPOINT)
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();

        //String BaseURL = "http://rap2api.taobao.org/app/mock/123041/";
        // baseURL = "http://www.baidu.com/";
        String baseUrl = "http://120.27.140.246:8081/sty_v2/";
        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(Service.class);
    }

    public static Api getInstance() {
        if (instance == null) {
            instance = new Api();
        }
        return instance;
    }



    public Observable<LoginRes> login( Map<String, String> maps){
        return service.login(maps);
    }

    public Observable<StationInfos> getStationForSelect(Map<String, String> maps){
        return service.getStationForSelect(maps);
    }


    
}
